package NetStorage;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created with IntelliJ IDEA.
 * User: Ratuil
 * Date: 19.03.13
 * Time: 12:23
 * To change this template use File | Settings | File Templates.
 */
public class NetStrorageServer {

    ServerSocket serverSocket = null;
    Socket socket = null;
    InputStream i = null;
    OutputStream o = null;
    List<Thread>clients=null;
    protected static ConcurrentMap<Integer,String> storage=new ConcurrentHashMap<Integer, String>();

    public static void main(String[] args) {
        new NetStrorageServer().run();
    }
    public void run(){
        try{
            try{
                serverSocket=new ServerSocket(6666);
                clients=new ArrayList<Thread>();
                while (true){
                    System.out.println("wait for connection");
                    socket=serverSocket.accept();
                    System.out.println("connection established: "+socket);
                    i=socket.getInputStream();
                    o=socket.getOutputStream();
                    Thread t=new Thread(new NetStrorageServerThread(i,o));
                    t.start();
                    clients.add(t);
                }
            }finally {
                i.close();
                o.close();
                socket.close();
                serverSocket.close();
                System.out.println("Connnection is closed");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
